from pygame import *
from config_reader import *
from tool import *


class Start:
    def __init__(self):
        init()
        self.screen = display.set_mode((650, 500))
        display.set_caption("默写器")
        self.screen.fill("lightskyblue")
        self.word = reader().word.get("start")
        self.button_font = font.SysFont("MicrosoftYaHei", 25, True, "black")
        self.text_font = font.SysFont("MicrosoftYaHei", 25, True, "black")
        self.title_font = font.SysFont("MicrosoftYaHei", 30, True, "black")
        self.tip_font = font.SysFont("MicrosoftYaHei", 15, True, "black")
        self.write_language = reader().write_language
        self.write_language_list = reader().write_language_all
        self.write_language_list_pos = 0
        self.stast_enter_band = 0
        self.contant = []
        self.enter_text_contant = ""

    def test_write_num_bool(self,filename):

        try:
            f = open(filename)
            #ans = f.read().split("\n")
            return True
        except:
            return False

    def config_ans_aks(self,filename):
        f = open("./resources/word/ans_{0}/".format(self.write_language) + filename+".txt")
        Tool().change_cfg("./config.cfg","write","truth_ans",Tool().pass_cfg(f.read().split("\n")))
        try:
            f = open("./resources/word/aks_{0}/".format(self.write_language) + filename + ".txt",encoding="uft-8")
        except:
            f = open("./resources/word/aks_{0}/".format(self.write_language) + filename + ".txt", encoding="gbk")
        Tool().change_cfg("./config.cfg", "write", "aks", Tool().pass_cfg(f.read().split("\n")))
        print("[system]:ready aks and ans from config.cfg")




    def main(self):
        self.setting_write = self.title_font.render(self.word.get("setting_write"),False, "black")
        self.setting_language = self.text_font.render(self.word.get("setting_language"),False, "black")
        # self.en = self.text_font.render(self.word.get("en"),False, "black", "pink")
        # self.jp = self.text_font.render(self.word.get("jp"),False, "black", "pink")
        self.back = self.text_font.render(self.word.get("back"),False, "black", "pink")
        self.start = self.text_font.render(self.word.get("start"),False, "black", "pink")
        self.write_num = self.text_font.render(self.word.get("write_num"), False, "black")
        self.enter_band_0 = image.load("./resources/image/enter_band_0.png").convert_alpha()
        self.enter_band_1 = image.load("./resources/image/enter_band_1.png").convert_alpha()
        self.write_language_button = self.button_font.render(self.word.get(self.write_language), False, "black", "pink")
        self.write_language = reader().write_language

        def pain():
            self.screen.fill("lightskyblue")
            self.write_language = self.write_language_list[self.write_language_list_pos]
            self.write_language_button = self.button_font.render(self.word.get(self.write_language), False, "black", "pink")
            self.enter_text_TS = ""

            for i in self.contant:
                self.enter_text_TS += i
            self.enter_text_contant = self.enter_text_TS
            Tool().change_cfg("./config.cfg","base","write_num",self.enter_text_contant)

            self.enter_text = self.text_font.render(self.enter_text_contant,False, "black")


            if(self.stast_enter_band %2 ==1):
                self.screen.blit(self.enter_band_1,(30 + self.write_num.get_width() + 5,165))
            else:
                self.screen.blit(self.enter_band_0, (30 + self.write_num.get_width() + 5, 165))

            if(not(self.test_write_num_bool("./resources/word/aks_{0}/".format(self.write_language) +self.enter_text_contant + ".txt")) and self.enter_text_contant != "" and self.enter_text_contant != " " ):
                self.errer_text = self.tip_font.render(self.word.get("errer"),False,"red")
                self.screen.blit(self.errer_text,(500,430))


            self.screen.blit(self.setting_write,(650/2 - self.setting_write.get_width()/2,5))
            self.screen.blit(self.setting_language, (30,90))
            self.screen.blit(self.write_language_button, (30+self.setting_language.get_width() + 5, 90))
            self.screen.blit(self.enter_text, (30+self.setting_language.get_width() + 7, 165))
            self.screen.blit(self.write_num, (30, 165))
            self.screen.blit(self.back, (20, 450))
            self.screen.blit(self.start, (500, 450))

        while True:
            for eve in event.get():
                if (eve.type == MOUSEBUTTONDOWN):
                    print("[system]:pos:{0}".format(eve.pos))
                    print("[system]:write_language_list_pos: {0}".format(self.write_language_list_pos))
                    print("[system]:write_language: {0}".format(self.write_language))
                    print("[system]:stast_enter_band: {0}".format(self.stast_enter_band))

                    if (self.stast_enter_band % 2 == 1 and not(Tool().button(self.enter_band_1,(30 + self.write_num.get_width() + 5, 165),eve.pos))):
                        self.stast_enter_band = 0

                    if(Tool().button(self.back,(20, 450),eve.pos)):
                        return "face"

                    if(Tool().button(self.start,(500, 450),eve.pos) and self.test_write_num_bool("./resources/word/aks_{0}/".format(self.write_language) +self.enter_text_contant + ".txt")):
                        self.config_ans_aks(self.enter_text_contant)
                        Tool().change_cfg("./config.cfg","base","write_language",self.write_language)
                        return "write"


                    if(Tool().button(self.write_language_button, (30+self.setting_language.get_width() + 5, 90),eve.pos)):
                        if(self.write_language_list_pos < len(self.write_language_list)-1):
                            self.write_language_list_pos += 1
                        else:
                            self.write_language_list_pos = 0

                    if(Tool().button(self.enter_band_1,(30 + self.write_num.get_width() + 5, 165),eve.pos)):
                        self.stast_enter_band += 1

                if(eve.type == KEYDOWN):
                    print("[system]:key down[{0}]".format(key.name(eve.key)))
                    if (key.name(eve.key) in Tool().key_str or key.name(eve.key) in Tool().keyint_str):
                        self.contant.append(key.name(eve.key))

                    if(key.name(eve.key) == "backspace"):
                        self.contant.remove(self.contant[len(self.contant)-1])

                    print("[system]:enterband contant: {0}".format(self.contant))





            pain()
            display.update()